import 'package:xm_app/common/config/Address.dart';
import 'package:xm_app/common/http/api_response.dart';
import 'package:xm_app/common/http/http_utils.dart';
import 'package:xm_app/model/Module.dart';
import 'package:xm_app/model/User.dart';
import 'DaoBase.dart';

class UserDao extends DaoBase {
  static UserDao _instance = UserDao._internal();

  factory UserDao() => _instance;

  UserDao._internal();
  Future<ApiResponse> register({String phone, String pwd}) async {
    var param = {"phone": phone, "pwd": pwd};
    return invoke(
      () => HttpUtils.post(Address.register(), data: param),
      creator: (jsonData) => Module.fromJson(jsonData),
    );
  }

  Future<ApiResponse> login({String phone, String pwd}) async {
    var param = {"phone": phone, "pwd": pwd};
    ApiResponse res = await invoke(() => HttpUtils.post(Address.login(), data: param));
    if (res.status == Status.ERROR) {
      return res;
    }
    //登陆成功
    await HttpUtils.setAuthorization(res.data);
    return res;
  }

  Future<ApiResponse> userInfo() async {
    return invoke<User>(() => HttpUtils.get(Address.userInfo()), creator: (d) => User.fromJson(d));
  }
}
